<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<table class="easyui-datagrid" id="brandlist" title="首页跑马灯"
	data-options="singleSelect:false,collapsible:true,pagination:true,url:'./brand/list',method:'get',pageSize:30,toolbar:'#brandtoolbar'">
	<thead>
		<tr>
			<th data-options="field:'ck',checkbox:true"></th>
			<th data-options="field:'id',width:120">图片ID</th>
			<th data-options="field:'name',width:200,editor:{type:'textbox',options:{prompt:'输入用户名'}}">图片名称</th>
			<th data-options="field:'photo',width:100,editor:{type:'textbox',options:{prompt:'输入密码'}},formatter:formatImages">图片效果</th>
			<th data-options="field:'created',width:130,align:'center',formatter:TAOTAO.formatDateTime">创建日期</th>
			<th data-options="field:'updated',width:130,align:'center',formatter:TAOTAO.formatDateTime">更新日期</th>
		</tr>
	</thead>
</table>
<div id="brandtoolbar" style="padding: 5px; height: auto">
	<div style="margin-bottom: 5px">
		<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="openUploadPic()">上传图片</a> 
		<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="deleteDate()">删除</a>
	</div>
	<div>
		<lable>图片名称:</lable>
		<input class="easyui-textbox" style="width: 280px; height: 25px"
			id="filename"> <a href="#" class="easyui-linkbutton"
			iconCls="icon-search" onclick="selectList()">查询</a>
	</div>
</div>
<div id="uploadPic" class="easyui-window" data-options="modal:true,closed:true,iconCls:'icon-save'" title="图片上传" style="width:400px;padding:30px 70px 50px 70px">
	<form id="uploadPicForm" method="post" enctype="multipart/form-data">
		<div style="margin-bottom:20px">
			<div>图片名称:</div>
			<input class="easyui-textbox" id="fileName" style="width:100%" data-options="required:true" >
		</div>
		<div style="margin-bottom:20px">
			<div>图片文件:</div>
			<input class="easyui-filebox" name="uploadFile" data-options="prompt:'选择图片文件',buttonText:'选择文件',required:true" style="width:100%">
		</div>
		<div>
			<input type="submit" value="上传"  class="easyui-linkbutton" style="width: 244px;height: 30px;">
		</div>
	</form>
</div>
<script>
	function formatImages(value) {
		if (value) {
			var s = '<a href="' + value +'"><img src="'+ value +'" style="width: 15px;height: 15px;"></a>';
			return s;
		} else {
			return '';
		}

	}
	function openUploadPic(){
		$("#uploadPic").window("open");
	}
	function deleteDate(){
		var ids = getSelectionsIds()
		$.ajax({
			 url:'./brand/deleteBrand',
			 method:'post',
			 data:{"ids":ids},
			 success:function(res){
					alert("成功") 
					$('#brandlist').datagrid('reload');
			 },
			 error:function(){
				alert("重新删除，操作失败") 
			 }
		 })
	}
	function getSelectionsIds(){
		var itemList = $("#brandlist");
		var sels = itemList.datagrid("getSelections");
		var ids = [];
		for(var i in sels){
			ids.push(sels[i].id);
		}
		ids = ids.join(",");
		return ids;
	}
	$(function(){
		$('#uploadPicForm').form({
			url:'./pic/upload',
			onSubmit:function(){
				return $(this).form('validate');
			},
			success:function(data){
				//data =eval(data)
				var jsonarray = eval('('+data+')');
				var params ={"name":$("#fileName").textbox("getValue"),"photo":jsonarray.url}
				if(jsonarray.error==0){
					$.post("./brand/addBrand",params, function(result){
            			if(result.status == 200){
            				$.messager.alert('提示','成功',undefined,function(){
            					$("#brandlist").datagrid("reload");
            					$("#uploadPic").window("close");
            				});
            			}
            		});
				}
			}
		});
	})
		function selectList(){
		var filename = $("#filename").textbox("getValue")	
		$('#brandlist').datagrid('load',{
			"filename": encodeURI(filename),
		});
	}
	
</script>